package cn.dankal.flyaudio.personal;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.jyj.recruitment.wxapi.PayUtils;
import com.jyj.recruitment.wxapi.WxConstants;
import com.kongzue.dialog.v2.SelectDialog;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import api.ShopUserServiceFactory;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.ApplyAfterSalesEntity;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.CreateOrderPreviewEntity;
import cn.dankal.basiclib.pojo.WexinPayResult;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.CallPhoneUtil;
import cn.dankal.basiclib.util.SharePreferenceManager;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.ProductsAdapter;
import cn.dankal.flyaudio.configs.PayConfigs;
import cn.dankal.flyaudio.dialog.CancelOrderDialog;
import cn.dankal.flyaudio.pop.AfterSalesPop;
import cn.dankal.flyaudio.pop.PayMethodPop;
import cn.dankal.flyaudio.store.LogisticsActivity;
import cn.dankal.flyaudio.store.PayResultActivity;
import cn.dankal.flyaudio.store.RefundDetailActivity;
import razerdp.basepopup.BasePopupWindow;

import static com.jyj.recruitment.wxapi.WxConstants.PAY_FAILED;
import static com.jyj.recruitment.wxapi.WxConstants.PAY_SUCCESS;

/**
 * 订单详情
 */
public class MyOrderDetailActivity extends BaseActivity implements View.OnClickListener {

    private TextView cancelView, tvEvaluate;

    private ImageView orderStateIconView;

    private TextView orderStateTextView;

    private TextView addressNameView;
    private TextView addressMobileView;
    private TextView addressDetailView;
    private TextView productAmountView;
    private TextView productFareView;
    private TextView productDiscountView;
    private TextView amountView;
    private TextView orderNumView;
    private TextView orderTimeView;
    private TextView orderPayTimeView;
    private TextView remarkView;
    private TextView shippingTimeView;
    private TextView completeTimeView;
    private RecyclerView recyclerview;
    private TextView titleView;
    private TextView commentView;
    private TextView afterSaleView;
    private TextView tvCallService;


    private View bottomCancelView;
    private View bottomPendingStoreView;
    private View bottomPendingPayView;
    private View bottomCompleteView;

    private ProductsAdapter adapter;
    private View bottomHandleView;
    private PayReceive receive = new PayReceive();

    private String orderUuid = "";

    private View logisticsView;

    private CreateOrderPreviewEntity orderDetail;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_order_detail;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("我的订单");
        orderUuid = getIntent().getStringExtra("orderUuid");
        cancelView = findViewById(R.id.tv_cancel);
        addressNameView = findViewById(R.id.tv_name);
        remarkView = findViewById(R.id.tv_order_remark);
        bottomCancelView = findViewById(R.id.fl_bottom_handle_cancel);
        bottomPendingStoreView = findViewById(R.id.fl_bottom_handle_pending_collection);
        bottomPendingPayView = findViewById(R.id.fl_bottom_handle_pending_pay);
        bottomCompleteView = findViewById(R.id.fl_bottom_handle_complete);
        completeTimeView = findViewById(R.id.tv_order_complete_time);
        findViewById(R.id.tv_confirm_collection).setOnClickListener(this);
        titleView = findViewById(R.id.tv_logistics_title);
        commentView = findViewById(R.id.tv_evaluate);
        afterSaleView = findViewById(R.id.tv_apply_refund);
        shippingTimeView = findViewById(R.id.tv_order_shipping_time);
        recyclerview = findViewById(R.id.recyclerview);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_cancel_pay).setOnClickListener(this);
        adapter = new ProductsAdapter();
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        recyclerview.setAdapter(adapter);
        commentView.setOnClickListener(this);
        productAmountView = findViewById(R.id.tv_product_amount);
        orderNumView = findViewById(R.id.tv_order_num);
        orderPayTimeView = findViewById(R.id.tv_order_pay_time);
        orderTimeView = findViewById(R.id.tv_order_time);
        productFareView = findViewById(R.id.tv_product_fare);
        productDiscountView = findViewById(R.id.tv_product_coupon);
        amountView = findViewById(R.id.tv_amount);
        addressMobileView = findViewById(R.id.tv_address_mobile);
        addressDetailView = findViewById(R.id.tv_address_detail);
        logisticsView = findViewById(R.id.rl_query_logistics_info);
        orderStateIconView = findViewById(R.id.iv_order_state);
        orderStateTextView = findViewById(R.id.tv_order_state);
        tvCallService = findViewById(R.id.tv_call_service);
        afterSaleView.setOnClickListener(this);
        tvCallService.setOnClickListener(this);
        findViewById(R.id.tv_pay).setOnClickListener(this);
        cancelView.setOnClickListener(this);
        logisticsView.setOnClickListener(this);
        getOrderDetail();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PAY_FAILED);
        intentFilter.addAction(PAY_SUCCESS);
        LocalBroadcastManager.getInstance(this).registerReceiver(receive, intentFilter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receive);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.tv_call_service:
                //联系客服
                SelectDialog.show(this, "是否需要拨打",
                        "400-9200-108", "是", (dialog2, which) -> {
                            CallPhoneUtil.callPhoneAndPermission(MyOrderDetailActivity.this, "400-9200-108");
                        }, "否", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        });
                break;
            case R.id.tv_cancel:
            case R.id.tv_cancel_pay:
                CancelOrderDialog dialog = new CancelOrderDialog();
                dialog.setListener(new CancelOrderDialog.DoSubmitListener() {
                    @Override
                    public void doSubmit(String reason) {
                        cancel(reason);
                    }
                });
                dialog.show(getSupportFragmentManager(), "");
                break;
            case R.id.tv_apply_refund:
                //申请售后
                new AfterSalesPop(this).setDoApplyListener(new AfterSalesPop.DoApplyListener() {
                    @Override
                    public void doApply(String reason, boolean isRefundOnlyMoney) {
                        ApplyAfterSalesEntity req = new ApplyAfterSalesEntity();
                        req.setUserReason(reason);
                        if (isRefundOnlyMoney) {
                            req.setType("RETURN_MONEY_ONLY");
                        } else {
                            req.setType("RETURN_MONEY_PRODUCT");
                        }
                        req.setOrderUuid(orderUuid);
                        ShopUserServiceFactory.applySales(req).subscribe(new AbstractSubscriber<BaseResponse<Object>>(MyOrderDetailActivity.this) {
                            @Override
                            public void onNext(BaseResponse<Object> objectBaseResponse) {
                                Intent intent = new Intent(MyOrderDetailActivity.this, RefundDetailActivity.class);
                                intent.putExtra("orderUuid", orderUuid);
                                startActivity(intent);
                                LiveEventBus.get("RefreshOrder", String.class).post("1");
                                finishActivity();

                            }
                        });
                    }
                }).setPopupGravity(Gravity.CENTER).showPopupWindow(findViewById(android.R.id.content));
                break;
            case R.id.tv_pay:
                PayMethodPop payMethodPop = new PayMethodPop(this);
                payMethodPop.setOnDismissListener(new BasePopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        int payMethod = payMethodPop.getPayMethod();
                        if (payMethod == PayConfigs.PAY_METHOD_ALIPAY) {
                            alipay(orderUuid);
                            return;
                        }
                        if (payMethod == PayConfigs.PAY_METHOD_WECHAT) {
                            wechatPay(orderUuid);
                            return;
                        }
                    }
                });
                payMethodPop.showPopupWindow(findViewById(android.R.id.content));

                break;
            case R.id.tv_evaluate:
                if (TextUtils.equals(orderDetail.getState(), MyOrderActivity.WAITING_SHIP)
                        || TextUtils.equals(orderDetail.getState(), MyOrderActivity.SHIPPING)) {
                    //联系客服
                    SelectDialog.show(this, "是否需要拨打",
                            "400-9200-108", "是", (dialog2, which) -> {
                                CallPhoneUtil.callPhoneAndPermission(MyOrderDetailActivity.this, "400-9200-108");
                            }, "否", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            });
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", orderDetail.getUuid());
                    if(orderDetail.getOrderItemList().size()>0) {

                        bundle.putString("model",
                                JSON.toJSONString(orderDetail.getOrderItemList()));
                    }
                    ActivityUtils.startActivity(bundle, MyCommentActivity.class);
                }
                break;
            case R.id.tv_confirm_collection:
                SelectDialog.show(this, "提示", "确认是否已收到货物", "确定", (dialog2, which) -> {
                    confirmReceiveStore(orderUuid);
                }, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                break;
            case R.id.rl_query_logistics_info:
                Bundle bundle1 = new Bundle();
                bundle1.putString("orderUuid", orderUuid);
                bundle1.putString("number",
                        orderDetail.getExpressNum());
                bundle1.putString("orderType", orderDetail.getExpressCorpName());
                bundle1.putString("type", "1");
                ActivityUtils.startActivity(bundle1, LogisticsActivity.class);
                break;
        }

    }

    private void confirmReceiveStore(String uuid) {
        showLoadingDialog();
        ShopUserServiceFactory.confirmReceive(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(this) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        dismissLoadingDialog();
                        getOrderDetail();
                    }
                });
    }

    public void wechatPay(String uuid) {
        showLoadingDialog();
        ShopUserServiceFactory.wx(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<WexinPayResult>>(this) {
                    @Override
                    public void onNext(BaseResponse<WexinPayResult> objectBaseResponse) {
                        dismissLoadingDialog();
                        IWXAPI iwxapi = WXAPIFactory.createWXAPI(FlyApplication.getContext(), objectBaseResponse.getData().getAppid(), true);
                        iwxapi.registerApp(objectBaseResponse.getData().getAppid());
                        SharePreferenceManager.setCachedAppKey(objectBaseResponse.getData().getAppid());
                        PayReq payReq = PayUtils.wechatPay(JSON.toJSONString(objectBaseResponse.getData()));
                        iwxapi.sendReq(payReq);
                    }
                });
    }

    public void alipay(String uuid) {
        showLoadingDialog();
        ShopUserServiceFactory.alipay(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<String>>(this) {
                    @Override
                    public void onNext(BaseResponse<String> createOrderPreviewEntityBaseResponse) {
                        dismissLoadingDialog();
                        Log.d("ConfirmOrderPresenter", JSON.toJSONString(createOrderPreviewEntityBaseResponse));
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                boolean isSuccess = PayUtils.aliPay(ActivityUtils.getTopActivity(), createOrderPreviewEntityBaseResponse.getData());
                                if (isSuccess) {
                                    Intent jump = new Intent(ActivityUtils.getTopActivity(), PayResultActivity.class);
                                    jump.putExtra("isPaySuccess", true);
                                    jump.putExtra("orderUuid", uuid);
                                    ActivityUtils.startActivity(jump);
                                    finishActivity();
                                    Log.d("ConfirmOrderPresenter", "支付成功");
                                } else {
                                    Intent jump = new Intent(ActivityUtils.getTopActivity(), PayResultActivity.class);
                                    jump.putExtra("isPaySuccess", false);
                                    jump.putExtra("orderUuid", uuid);
                                    ActivityUtils.startActivity(jump);
                                    finishActivity();
                                    Log.d("ConfirmOrderPresenter", "支付失败");
                                }
                            }
                        }).start();
                    }
                });
    }

    /**
     * 已取消订单
     */
    public void showOrderCancel() {
        orderStateIconView.setImageResource(R.mipmap.icon_jiaoyuwancheng);
        orderStateTextView.setText("交易关闭");
        addressNameView.setText(orderDetail.getUserAddress().getContacter());
        addressMobileView.setText(orderDetail.getUserAddress().getPhone());
        addressDetailView.setText("地址: " + orderDetail.getUserAddress().getProvince() + orderDetail.getUserAddress().getCity() + orderDetail.getUserAddress().getRegion() + orderDetail.getUserAddress().getAddressDetail());
        productAmountView.setText("¥" + orderDetail.getOriginPrice());
        productFareView.setText("¥" + orderDetail.getShippingPrice());
        productDiscountView.setText("¥" + orderDetail.getDiscountedUnitPrice());
        amountView.setText(orderDetail.getUsedScore() + "积分+¥" + orderDetail.getNeedPay());
        orderTimeView.setText("创建时间：" + orderDetail.getCreatedDatetime());
        orderNumView.setText("订单编号: " + orderDetail.getUuid());
        adapter.addDatas(orderDetail.getOrderItemList(), true);
    }

    /**
     * 已完成订单
     */
    public void showOrderClose() {
        orderStateIconView.setImageResource(R.mipmap.icon_jiaoyuwancheng);
        orderStateTextView.setText("交易完成");
        logisticsView.setVisibility(View.VISIBLE);
        titleView.setText("感谢您的支持，期待下次交易");
        completeTimeView.setVisibility(View.VISIBLE);
        completeTimeView.setText("成交时间: " + orderDetail.getReceivedDatetime());
        addressNameView.setText(orderDetail.getUserAddress().getContacter());
        addressMobileView.setText(orderDetail.getUserAddress().getPhone());
        addressDetailView.setText("地址: " + orderDetail.getUserAddress().getProvince() + orderDetail.getUserAddress().getCity() + orderDetail.getUserAddress().getRegion() + orderDetail.getUserAddress().getAddressDetail());
        productAmountView.setText("¥" + orderDetail.getOriginPrice());
        productFareView.setText("¥" + orderDetail.getShippingPrice());
        productDiscountView.setText("¥" + orderDetail.getDiscountedUnitPrice());
        amountView.setText(orderDetail.getUsedScore() + "积分+¥" + orderDetail.getNeedPay());
        orderTimeView.setText("创建时间：" + orderDetail.getCreatedDatetime());
        orderNumView.setText("订单编号: " + orderDetail.getUuid());
        orderPayTimeView.setVisibility(View.VISIBLE);
        orderPayTimeView.setText("付款时间：" + orderDetail.getPaidDatetime());
        bottomCompleteView.setVisibility(View.VISIBLE);
        commentView.setVisibility(View.GONE);
        adapter.addDatas(orderDetail.getOrderItemList(), true);
    }

    /**
     * 已完成待评价订单
     */
    public void showOrderComplete() {
        orderStateIconView.setImageResource(R.mipmap.icon_jiaoyuwancheng);
        orderStateTextView.setText("交易完成");
        logisticsView.setVisibility(View.VISIBLE);
        titleView.setText("感谢您的支持，期待下次交易");
        completeTimeView.setVisibility(View.VISIBLE);
        completeTimeView.setText("成交时间: " + orderDetail.getReceivedDatetime());
        addressNameView.setText(orderDetail.getUserAddress().getContacter());
        addressMobileView.setText(orderDetail.getUserAddress().getPhone());
        addressDetailView.setText("地址: " + orderDetail.getUserAddress().getProvince() + orderDetail.getUserAddress().getCity() + orderDetail.getUserAddress().getRegion() + orderDetail.getUserAddress().getAddressDetail());
        productAmountView.setText("¥" + orderDetail.getOriginPrice());
        productFareView.setText("¥" + orderDetail.getShippingPrice());
        productDiscountView.setText("¥" + orderDetail.getDiscountedUnitPrice());
        amountView.setText(orderDetail.getUsedScore() + "积分+¥" + orderDetail.getNeedPay());
        orderTimeView.setText("创建时间：" + orderDetail.getCreatedDatetime());
        orderNumView.setText("订单编号: " + orderDetail.getUuid());
        orderPayTimeView.setVisibility(View.VISIBLE);
        orderPayTimeView.setText("付款时间：" + orderDetail.getPaidDatetime());
        bottomCompleteView.setVisibility(View.VISIBLE);
        commentView.setText("待评价");
        bottomPendingStoreView.setVisibility(View.GONE);
        adapter.addDatas(orderDetail.getOrderItemList(), true);
    }

    /**
     * 待发货订单
     */
    public void showOrderPendingSendStore() {
        orderStateIconView.setImageResource(R.mipmap.ic_daishouhuo);
        orderStateTextView.setText("等待卖家发货");
        addressNameView.setText(orderDetail.getUserAddress().getContacter());
        addressMobileView.setText(orderDetail.getUserAddress().getPhone());
        addressDetailView.setText("地址: " + orderDetail.getUserAddress().getProvince() + orderDetail.getUserAddress().getCity() + orderDetail.getUserAddress().getRegion() + orderDetail.getUserAddress().getAddressDetail());
        productAmountView.setText("¥" + orderDetail.getOriginPrice());
        productFareView.setText("¥" + orderDetail.getShippingPrice());
        productDiscountView.setText("¥" + orderDetail.getDiscountedUnitPrice());
        amountView.setText(orderDetail.getUsedScore() + "积分+¥" + orderDetail.getNeedPay());
        orderTimeView.setText("创建时间：" + orderDetail.getCreatedDatetime());
        orderNumView.setText("订单编号: " + orderDetail.getUuid());
        orderPayTimeView.setVisibility(View.VISIBLE);
        orderPayTimeView.setText("付款时间：" + orderDetail.getPaidDatetime());
        bottomCancelView.setVisibility(View.GONE);
        bottomCompleteView.setVisibility(View.VISIBLE);
        commentView.setText("联系客服");
        adapter.addDatas(orderDetail.getOrderItemList(), true);
    }

    /**
     * 待收货订单
     */
    public void showPendingCollection() {
        orderStateIconView.setImageResource(R.mipmap.ic_daifahuo);
        orderStateTextView.setText("卖家已发货");
        addressNameView.setText(orderDetail.getUserAddress().getContacter());
        addressMobileView.setText(orderDetail.getUserAddress().getPhone());
        addressDetailView.setText("地址: " + orderDetail.getUserAddress().getProvince() + orderDetail.getUserAddress().getCity() + orderDetail.getUserAddress().getRegion() + orderDetail.getUserAddress().getAddressDetail());
        productAmountView.setText("¥" + orderDetail.getOriginPrice());
        productFareView.setText("¥" + orderDetail.getShippingPrice());
        productDiscountView.setText("¥" + orderDetail.getDiscountedUnitPrice());
        amountView.setText(orderDetail.getUsedScore() + "积分+¥" + orderDetail.getNeedPay());
        orderTimeView.setText("创建时间：" + orderDetail.getCreatedDatetime());
        orderNumView.setText("订单编号: " + orderDetail.getUuid());
        orderPayTimeView.setVisibility(View.VISIBLE);
        orderPayTimeView.setText("付款时间：" + orderDetail.getPaidDatetime());
        bottomPendingStoreView.setVisibility(View.VISIBLE);
        adapter.addDatas(orderDetail.getOrderItemList(), true);
        logisticsView.setVisibility(View.VISIBLE);
        shippingTimeView.setVisibility(View.VISIBLE);
//        bottomCompleteView.setVisibility(View.VISIBLE);
        commentView.setText("联系客服");
        shippingTimeView.setText("发货时间: " + orderDetail.getShippedDatetime());
    }

    /**
     * 待支付订单
     */
    public void showPendingPay() {
        orderStateIconView.setImageResource(R.mipmap.icon_daifukuan);
        orderStateTextView.setText("等待买家付款");
        addressNameView.setText(orderDetail.getUserAddress().getContacter());
        addressMobileView.setText(orderDetail.getUserAddress().getPhone());
        addressDetailView.setText("地址: " + orderDetail.getUserAddress().getProvince() + orderDetail.getUserAddress().getCity() + orderDetail.getUserAddress().getRegion() + orderDetail.getUserAddress().getAddressDetail());
        productAmountView.setText("¥" + orderDetail.getOriginPrice());
        productFareView.setText("¥" + orderDetail.getShippingPrice());
        productDiscountView.setText("¥" + orderDetail.getDiscountedUnitPrice());
        amountView.setText(orderDetail.getUsedScore() + "积分+¥" + orderDetail.getNeedPay());
        orderTimeView.setText("创建时间：" + orderDetail.getCreatedDatetime());
        orderNumView.setText("订单编号: " + orderDetail.getUuid());
        orderPayTimeView.setVisibility(View.GONE);
        bottomPendingPayView.setVisibility(View.VISIBLE);
        adapter.addDatas(orderDetail.getOrderItemList(), true);
    }

    /**
     * 取消订单操作
     */
    private void cancel(String reason) {
        showLoadingDialog();
        ShopUserServiceFactory.cancelOrder(orderUuid, reason)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(this) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        dismissLoadingDialog();
                        ToastUtils.showShort("取消成功");
                        LiveEventBus.get("RefreshOrder", String.class).post("");
                        finishActivity();
                    }
                });
    }

    /**
     * 获取订单详情
     */
    private void getOrderDetail() {
        showLoadingDialog();
        ShopUserServiceFactory.getOrderDetails(orderUuid)
                .subscribe(new AbstractSubscriber<BaseResponse<CreateOrderPreviewEntity>>(this) {
                    @Override
                    public void onNext(BaseResponse<CreateOrderPreviewEntity> createOrderPreviewEntityBaseResponse) {
                        dismissLoadingDialog();
                        CreateOrderPreviewEntity data = createOrderPreviewEntityBaseResponse.getData();
                        orderDetail = data;
                        remarkView.setText("订单备注:" + createOrderPreviewEntityBaseResponse.getData().getUserMessage());
                        if (TextUtils.equals(data.getState(), MyOrderActivity.WAITING_PAY)) {
                            showPendingPay();
                        } else if (TextUtils.equals(data.getState(), MyOrderActivity.WAITING_SHIP)) {
                            showOrderPendingSendStore();
                        } else if (TextUtils.equals(data.getState(), MyOrderActivity.SHIPPING)) {
                            showPendingCollection();
                        } else if (TextUtils.equals(data.getState(), MyOrderActivity.WAITING_COMMENT)) {
                            showOrderComplete();
                        } else if (TextUtils.equals(data.getState(), MyOrderActivity.SUCCESS)) {
                            showOrderClose();
                        } else if (TextUtils.equals(data.getState(), MyOrderActivity.AFTER_SALE)) {
                            showOrderClose();
                        } else if (TextUtils.equals(data.getState(), MyOrderActivity.USER_CANCELLED)
                                || TextUtils.equals(data.getState(), MyOrderActivity.TIMEOUT_CANCELLED)
                        ||TextUtils.equals(data.getState(), MyOrderActivity.CLOSE)) {
                            showOrderCancel();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        finish();
                    }
                });
    }

    /**
     * 支付结果回调
     */
    class PayReceive extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (TextUtils.equals(intent.getAction(), WxConstants.PAY_SUCCESS)) {
                Intent jump = new Intent(ActivityUtils.getTopActivity(), PayResultActivity.class);
                jump.putExtra("isPaySuccess", true);
                jump.putExtra("orderUuid", orderUuid);
                ActivityUtils.startActivity(jump);
                finish();

            } else if (TextUtils.equals(intent.getAction(), PAY_FAILED)) {
                Intent jump = new Intent(ActivityUtils.getTopActivity(), PayResultActivity.class);
                jump.putExtra("isPaySuccess", false);
                jump.putExtra("orderUuid", orderUuid);
                ActivityUtils.startActivity(jump);
                finish();
            }
        }
    }
}
